﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Baza.EF.DAL;
using Baza.EF.BLL;

using System.Net;
using System.Net.Mail;

namespace Pep_3014_3070.Anonymous
{
    public partial class NewCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCountry();
            }
        }
        private void clearArea()
        {
            FirstNameInput.Text = "";
            LastNameInput.Text = "";
            EmailInput.Text = "";
            CountryDropDownList.SelectedIndex = 0;
        }
        private void BindCountry()
        {
            List<Countries> temp = DARegistration.getAllCountres();
            Countries c = new Countries();
            c.Country_name = "Choose country";
            c.CountryID = 0;
            temp.Insert(0, c);

            CountryDropDownList.DataSource = temp;
            CountryDropDownList.DataTextField = "Country_name";
            CountryDropDownList.DataValueField = "CountryID";
            CountryDropDownList.DataBind();
        }

        protected void RegisterButton_Click(object sender, EventArgs e)
        {
            if (FirstNameInput.Text != "" && LastNameInput.Text != "" && EmailInput.Text != "" && PasswordInput.Text != "" && ConfirmPasswordInput.Text != ""
                && Convert.ToInt32(CountryDropDownList.SelectedIndex) != 0 && Address1Input.Text != "" && Address2Input.Text != ""
                && CityInput.Text != "" && StreetNumberInput.Text != ""

                )
            {

                if (DACustomers.checkEmail(EmailInput.Text.Trim()))
                {

                    Baza.EF.DAL.Customers c = new Baza.EF.DAL.Customers();
                    c.FirstName = FirstNameInput.Text;
                    c.LastName = LastNameInput.Text;
                    c.Email = EmailInput.Text;
                    c.PasswordSalt = UIHelper.GenerateSalt();
                    c.PasswordHash = UIHelper.GenerateHash(PasswordInput.Text, c.PasswordSalt);
                    c.Phone = PhoneInput.Text;

                    Address ad = new Address();
                    ad.Address1 = Address1Input.Text;
                    ad.Address2 = Address2Input.Text;
                    ad.StreetNumber = Convert.ToInt32(StreetNumberInput.Text);


                    Cities cit = new Cities();
                    cit.Name = CityInput.Text;
                    cit.CountryID = Convert.ToInt32(CountryDropDownList.SelectedIndex);

                    int id = DARegistration.RegisterCustomers(c, ad, cit);
                    //NAPRAVITI REDIRECT NA PROFILE EDIT PAGE  . . .
                    email_send(c.FirstName + " " + c.LastName, c.Email, "Aktivacija racuna", id);
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Aktivacijski link je poslan na Vasu email adresu.');", true);
                    clearArea();

                    string url = string.Format("/ProfileCustomer/{0}", id);
                    if (url != "" || url != null)
                        Response.Redirect(url);




                }
                else
                    AlertMessage("Email adresa je vec zauzeta!");
            }
        }
        private void AlertMessage(string errorMsg)
        {

            string message = "alert('" + errorMsg + "')";
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "alert", message, true);


        }

        public bool email_send(string ime, string email, string naslov, int CustomerID)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.Subject = naslov;
                mail.To.Add(email);
                mail.From = new MailAddress("pep3031@hotmail.com");

                mail.Body = "Šalje <b>" + ime + "</b>,";
                    mail.Body += "<br> E-mail: <b>" + email + "</br>";

                    string ActivationUrl = Server.HtmlEncode("http://localhost:53358/ActivateAccount/" + CustomerID + "-" + email);

                mail.Body += 
                      "Za uspjesnu  registraciju Vaseg klijentskog racuna "+
                      "potrebno je  kliknuti <a href='" + ActivationUrl + "'> ovdje</a>. \nHvala!";
                mail.IsBodyHtml = true;

                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.live.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.Credentials = new System.Net.NetworkCredential
                    ("elvir_registracije@live.com", "5.11.(L).-elvir-");
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Timeout = 100000;
                smtp.Send(mail);
                return true;
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Greska se desila : " + ex.Message.ToString() + "');", true);
                return false;
            }
        }


    }
}